<?php

// +----------------------------------------------------------------------
// | Shop-Demo for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2022~2023 Anyon <zoujingli@qq.com>
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// | 会员免费 ( https://thinkadmin.top/vip-introduce )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\data\controller\user;

use app\data\model\DataQiyeRzinfo as DataNewsItem;
use app\data\service\NewsService;
use think\admin\Controller;
use think\admin\extend\CodeExtend;
use think\admin\helper\QueryHelper;
use think\facade\Db;

/**
 * 企业认证信息管理
 * Class Item
 * @package app\data\controller\news
 */
class Qiyerzinfo extends Controller
{
    /**
     * 企业认证
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->type = $this->get['type'] ?? 'index';

        $this->qyszd_list = Db::name('data_qyszd')->where('deleted',0)->order('sort desc')->select()->toArray();

        DataNewsItem::mQuery($this->get)->layTable(function () {
            $this->title = '企业认证记录';
        }, function (QueryHelper $query) {
            $query->like('qymc,tyxydm,sbr_xm,sbr_sfzh,sbr_phone')->equal('qyszd_id')->dateBetween('create_at');
            $query->where(['status' => intval($this->type === 'index'), 'deleted' => 0]);
        });
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _page_filter(array &$data)
    {
        foreach ($data as &$item){
            if(!empty($item['sbr_type'])){
                $item['sbr_type_txt'] = Db::name("data_sbr_type")->where('id',$item['sbr_type'])->value('name');
            }else{
                $item['sbr_type_txt'] = '';
            }


            if(!empty($item['qyszd_id'])){
                $item['qyszd_txt'] = Db::name("data_qyszd")->where('id',$item['qyszd_id'])->value('name');
            }else{
                $item['qyszd_txt'] = '';
            }

            if(!empty($item['qybq'])){
                $qybq_array = explode(',',$item['qybq']);
                $list = Db::name("data_qiye_biaoqian")->whereIn('id',$qybq_array)->select()->toArray();
                $item['qybq_txt'] = '';
                $i = 0;
                foreach ($list as $qybq){
                    if($i>0){
                        $item['qybq_txt'] .= ',';
                    }

                    $item['qybq_txt'] .= $qybq['name'];

                    $i++;
                }

            }else{
                $item['qybq_txt'] = '';
            }


            if(!empty($item['fwg_ids'])){
                $qybq_array = explode(',',$item['fwg_ids']);
                $list = Db::name("data_user")->whereIn('id',$qybq_array)->where('sf',2)->select()->toArray();
                $item['fwy_txt'] = '';
                $i = 0;
                foreach ($list as $qybq){
                    if($i>0){
                        $item['fwy_txt'] .= ',';
                    }

                    $item['fwy_txt'] .= $qybq['nickname'];

                    $i++;
                }

            }else{
                $item['fwy_txt'] = '';
            }
        }

    }

    /**
     * 添加文章内容
     * @auth true
     */
    public function add()
    {
        $this->title = '添加内容';
        DataNewsItem::mForm('form');
    }

    /**
     * 编辑文章内容
     * @auth true
     */
    public function edit()
    {
        $this->title = '编辑内容';
        DataNewsItem::mForm('form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if (empty($data['code'])) {

        }
        if ($this->request->isGet()) {

        } else {

        }
    }

    /**
     * 表单结果处理
     * @param boolean $state
     */
    protected function _form_result(bool $state)
    {
        if ($state) {
            $this->success('保存成功！', 'javascript:history.back()');
        }
    }

    /**
     * 修改文章状态
     * @auth true
     */
    public function state()
    {
        DataNewsItem::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除文章内容
     * @auth true
     */
    public function remove()
    {
        DataNewsItem::mDelete();
    }

    /**
     * 文章内容选择
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function select()
    {
        $this->get['status'] = 1;
        $this->index();
    }


    public function shbh()
    {
        $id = input('id');

        //开启事务
        Db::startTrans();
        try {


            Db::name("data_qiye_rzinfo")->where('id',$id)->update(['deleted'=>1]);

            $qiye_info = Db::name("data_qiye_rzinfo")->where('id',$id)->find();


            $dsh_id = Db::name("data_qiye_rzinfo_dsh")->where('user_id',$qiye_info['user_id'])->where('qymc',$qiye_info['qymc'])
                ->where('sh_status',1)
                ->order('id desc')->value('id');


            Db::name("data_qiye_rzinfo_dsh")->where('id',$dsh_id)->update(['sh_status'=>0]);

            Db::commit();

//            $this->success('操作成功！', 'javascript:history.back()');
        }
        catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error('操作失败');
        }
    }

}